﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using AimGameLibrary.GameObject;
namespace AimGameLibrary.ScreenManager
{
    public struct ScreenResolution
    {
        public const String VGA     = "640  x 480  VGA    (4:3)";
        public const String SVGA    = "800  x 600  SVGA   (4:3)";
        public const String XGA     = "1024 x 768  XGA    (4:3)";
        public const String WXGA    = "1280 x 800  WXGA   (16:10)";
        public const String WSXGA   = "1440 x 900  WSXGA  (16:10)";
        public const String HD720   = "1280 x 720  HD720  (16:9)";
        public const String HD      = "1366 x 768  HD     (16:9)";
        public const String HDPlus  = "1600 x 900  HDPlus (16:9)";
        public const String HD1080  = "1920 x 1080 HD1080 (16:9)";
        
        public static Point GetResolution(String inputResolution)
        {
            switch (inputResolution)
            {
                case VGA    : return new Point(640, 480);
                case SVGA   : return new Point(800, 600);
                case XGA    : return new Point(1024, 768);
                case WXGA   : return new Point(1280, 800);
                case WSXGA  : return new Point(1440, 900);
                case HD720  : return new Point(1280, 720);
                case HD     : return new Point(1366, 768);
                case HDPlus : return new Point(1600, 900);
                case HD1080 : return new Point(1920, 1080);
                default: return Point.Zero;
            }
        }

        public static List<String> GetListResolution()
        {
            
            List<String> temp = new List<string>();
            temp.Add(VGA);
            temp.Add(SVGA);
            temp.Add(XGA);
            temp.Add(WXGA);
            temp.Add(WSXGA);
            temp.Add(HD720);
            temp.Add(HD);
            temp.Add(HDPlus);
            temp.Add(HD1080);
            return temp;
        }

        public static String GetNameResolution(Point inputpoint)
        {
            foreach (String str in GetListResolution())
            {
                if (GetResolution(str) == inputpoint)
                    return str;
            }
            return VGA;
        }
    }

    public class AimGameScreen : GameScreen
    {
        ContentManager content;
        String NameScreen;
        protected List<BaseGameObject> Listbasegameobejct;
        protected Dictionary<String, BaseGameObject> DictionaryGameObject;

        protected List<IDraw> ListDraw;
        protected List<IUpdateMainTime> ListUpdateGameTime;
        protected List<IHandleInputUpdate> ListHandleInputUpdate;

        //protected InputState Maininputstate;
        protected Camera camera;

        AimGameControl aimgamecontrol;

        public AimGameScreen(String NameScreen)
        {
            this.NameScreen = NameScreen;
        }

        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");
            
            //gameFont = content.Load<SpriteFont>("gamefont");
            if(NameScreen == null)
                this.NameScreen = ScreenManager.MainscripT.GetFindStartScreen();

            #region Setup FaceTime
            double[] FaceTime = ScreenManager.MainscripT.GetFaceTimeScreen(NameScreen);

            if (FaceTime != null)
            {
                if (FaceTime.Length == 2)
                {
                    TransitionOnTime = TimeSpan.FromSeconds(FaceTime[0]);
                    TransitionOffTime = TimeSpan.FromSeconds(FaceTime[1]);
                }
                else if (FaceTime.Length == 1)
                {
                    TransitionOnTime = TimeSpan.FromSeconds(FaceTime[0]);
                }
            }
            else
            {
                TransitionOnTime = TimeSpan.FromSeconds(0.5);
                TransitionOffTime = TimeSpan.FromSeconds(0.5);
            }
            #endregion
      
            #region New List
            aimgamecontrol = new AimGameControl();
            aimgamecontrol.CamerA.MymodE = RunMode.GamePlayMode;
            aimgamecontrol.CamerA.ResolutioN = new Point(ScreenManager.GraphicsDevice.Viewport.Width, ScreenManager.GraphicsDevice.Viewport.Height);
            aimgamecontrol.ListUpdateTimE.Add(aimgamecontrol.CamerA);
            #endregion
            
            /// Load script
            ScreenManager.MainscripT.LoadContent(content, ScreenManager.SpriteBatch,this);
            ScreenManager.MainscripT.RunLoadObject(NameScreen, aimgamecontrol);
            
            // A real game would probably have more content than this sample, so
            // it would take longer to load. We simulate that by delaying for a
            // while, giving you a chance to admire the beautiful loading screen.
            //Thread.Sleep(1000);

            // once the load has finished, we use ResetElapsedTime to tell the game's
            // timing mechanism that we have just finished a very long frame, and that
            // it should not try to catch up.
            ScreenManager.Game.ResetElapsedTime();
        }

        public override void UnloadContent()
        {
            base.UnloadContent();
            aimgamecontrol.UnLoadContent();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            if (this.IsActive)
            {
                ActiveUpdate(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            }
        }

        public virtual void ActiveUpdate(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            aimgamecontrol.UpdateGameTime(gameTime);
        }

        public override void HandleInput(InputState input, GameTime gametime)
        {
            base.HandleInput(input, gametime);

            aimgamecontrol.HandleInput(input, gametime);
        }

        public override void Draw(GameTime gameTime)
        {
            //base.Draw(gameTime);

            // This game has a blue background. Why? Because!
            ScreenManager.GraphicsDevice.Clear(ClearOptions.Target,
                                               Color.CornflowerBlue, 0, 0);

            // Our player and enemy are both actually just text strings.
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied);

            //if(NameScreen!=null)
            //    spriteBatch.DrawString(ScreenManager.Font, NameScreen + " | " + TransitionAlpha, Vector2.Zero, Color.White);

            aimgamecontrol.Draw();

            spriteBatch.End();

            // If the game is transitioning on or off, fade it out to black.
            if (TransitionPosition > 0)
                ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);
        }
    }
}
